package com.ruoyi.hov;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.DataNotFindException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.hov.entity.Hov;
import com.ruoyi.hov.service.HovService;

import java.io.Serializable;
import java.util.Objects;

/**
 * **********************
 * * Author: XiaShiLong *
 * * Date  : 2021-04-01 *
 * * Time  : 08:26      *
 * * to    : lz&xm      *
 * **********************
 * hov  工具类
 **/
public class HovUtils {

    private static final String REDIS_KEY = "sys_hov:";

    /**
     * 设置字典缓存
     *
     * @param key 参数键
     * @param hov hov数据列表
     */
    public static void setHovCache(Serializable key, Hov hov) {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), hov);
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     * @return hov hov数据列表
     */
    public static Hov getHovCache(Serializable key) {
        Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
        if (Objects.nonNull(cacheObj)) {
            return StringUtils.cast(cacheObj);
        } else {
            Hov hov = SpringUtils.getBean(HovService.class).getById(key);
            if (Objects.nonNull(hov)) {
                setHovCache(key, hov);
                return hov;
            }
        }
        throw new DataNotFindException("未获取hov!");
    }

    /**
     * 删除key
     *
     * @param configKey key关键字
     */
    public static void clearHovCache(Serializable configKey) {
        SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(configKey));
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(Serializable configKey) {
        return REDIS_KEY + configKey;
    }
}
